package com.mywiki.controller;
import com.mywiki.req.CategoryQueryReq;
import com.mywiki.req.CategorySaveReq;
import com.mywiki.resp.CategoryQueryResp;
import com.mywiki.resp.CommonResp;
import com.mywiki.resp.PageResp;
import com.mywiki.service.CategoryService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * describe:
 *
 * @author: robin
 * @date 2024/1/19 11:19
 */
@RestController
@RequestMapping("/category")
public class CategoryController {

    private static final Logger LOG = LoggerFactory.getLogger(CategoryController.class);

    @Autowired
    private CategoryService categoryService;

    @GetMapping("/list")
    public CommonResp list(@Valid CategoryQueryReq req){
        LOG.info("req:{}", req);
        CommonResp<PageResp<CategoryQueryResp>> resp = new CommonResp<>();
        PageResp<CategoryQueryResp> list = categoryService.list(req);
        resp.setContent(list);
        return resp;
    }

    @PostMapping("/save")
    public CommonResp save(@Valid @RequestBody CategorySaveReq req){
        CommonResp resp = new CommonResp();
        categoryService.save(req);
        return resp;
    }

    @DeleteMapping("/delete/{id}")
    public CommonResp delete(@PathVariable String id){
        CommonResp resp = new CommonResp();
        categoryService.delete(Long.valueOf(id));
        LOG.info("删除完成:{}", id);
        return resp;
    }

    @GetMapping("/all")
    public CommonResp listAll(){
        CommonResp<List<CategoryQueryResp>> resp = new CommonResp<>();
        List<CategoryQueryResp> list = categoryService.listAll();
        resp.setContent(list);
        return resp;
    }


}
